package mapper

import (
	"github.com/beego/beego/v2/client/orm"
	context2 "golang.org/x/net/context"
	"ksd-social-api/commons/page"
	"ksd-social-api/modules/special/model"
	"ksd-social-api/modules/special/model/context"
)

type SpecialCommentReplyMapper struct{}

/**
 * 保存专题评论
 * @author feige
 * @date 2023-11-03
 * @version 1.0
 * @desc
 */
func (mapper *SpecialCommentReplyMapper) SaveCommentReply(comment *model.SpecialCommentReply) bool {

	// 创建orm对象
	err := orm.NewOrm().DoTx(func(ctx context2.Context, txOrm orm.TxOrmer) (err error) {
		_, err = txOrm.Insert(comment)
		_, err = txOrm.Raw("update  xk_special_comment set reply_num =  reply_num + 1 where id = ? and reply_num >= 0 and system_id = ?", comment.ParentId, comment.SystemId).Exec()
		return err
	})

	return err == nil
}

/**
* 查找专题分类
* @author feige
* @date 2023-11-03
* @version 1.0
* @desc
 */
func (mapper *SpecialCommentReplyMapper) FindCommentPage(ctx context.CommentReplyPageContext) (p *page.Page, err error) {
	// 1：准备一个专题数据容器
	commentReplyList := []model.SpecialCommentReply{}
	// 获取数据库链接
	mysql := orm.NewOrm()
	// 查询数据库表--------------------------------------------开始
	qs := mysql.QueryTable("xk_special_comment_reply")
	// 设定查询条件-------------------------------------------过滤
	whereCondition := orm.NewCondition().
		And("opid", ctx.Opid.Uint64()).
		And("system_id", ctx.SystemId).
		And("status", 1).
		And("parent_id", ctx.ParentId.Uint64())

	// 根据查询条件求总数 --------------------------------------------------------结束 Count() ---All()
	total, _ := qs.SetCond(whereCondition).Count()
	// 开始根据总数和用户传递进来的分页信息，开始计算sql所需要的分页信息
	page := p.Page(ctx.PageNo, ctx.PageSize, total)
	_, err1 := qs.SetCond(whereCondition).OrderBy("-create_time").Limit(page.PageSize, page.CurrentPage).
		All(&commentReplyList,
			"id",
			"user_id",
			"username",
			"nickname",
			"vip_type",
			"uuid",
			"avatar",
			"create_time",
			"content",
			"score",
			"parent_id",
			"pid",
			"opid",
			"title",
		)
	if err1 != nil {
		return nil, err
	}
	page.Records = commentReplyList
	return page, nil
}

/**
* 查找专题分类
* @author feige
* @date 2023-11-03
* @version 1.0
* @desc
 */
func (mapper *SpecialCommentReplyMapper) FindCommentPageMain(ctx context.CommentReplyPageContextMain) (p *page.Page, err error) {
	// 1：准备一个专题数据容器
	commentReplyList := []model.SpecialCommentReply{}
	// 获取数据库链接
	mysql := orm.NewOrm()
	// 查询数据库表--------------------------------------------开始
	qs := mysql.QueryTable("xk_special_comment_reply")
	// 设定查询条件-------------------------------------------过滤
	whereCondition := orm.NewCondition().
		And("opid", ctx.Opid).
		And("system_id", ctx.SystemId).
		And("status", 1).
		And("parent_id", ctx.ParentId)

	// 根据查询条件求总数 --------------------------------------------------------结束 Count() ---All()
	total, _ := qs.SetCond(whereCondition).Count()
	// 开始根据总数和用户传递进来的分页信息，开始计算sql所需要的分页信息
	page := p.Page(ctx.PageNo, ctx.PageSize, total)
	_, err1 := qs.SetCond(whereCondition).OrderBy("-create_time").Limit(page.PageSize, page.CurrentPage).
		All(&commentReplyList,
			"id",
			"user_id",
			"username",
			"nickname",
			"vip_type",
			"uuid",
			"avatar",
			"create_time",
			"content",
			"score",
			"parent_id",
			"pid",
			"opid",
			"title",
		)
	if err1 != nil {
		return nil, err
	}
	page.Records = commentReplyList
	return page, nil
}

/**
 * 根据用户ID和ID删除评论
 * @author feige
 * @date 2023-11-03
 * @version 1.0
 * @desc
 */
func (mapper *SpecialCommentReplyMapper) DelCommentById(id uint64, parentId uint64, opid uint64, userId uint64, systemId uint64) bool {
	// 创建orm对象
	err := orm.NewOrm().DoTx(func(ctx context2.Context, txOrm orm.TxOrmer) (err error) {
		_, err = txOrm.Raw("delete from xk_special_comment_reply where id = ? and parent_id = ? and opid=? and user_id = ? and system_id = ?", id, parentId, opid, userId, systemId).Exec()
		_, err = txOrm.Raw("update  xk_special_comment set reply_num =  reply_num - 1 where id = ? and reply_num > 0 and system_id = ?", parentId, systemId).Exec()
		return err
	})
	return err == nil
}

/**
 * 根据parentId删除所有子回复
 * @author feige
 * @date 2023-11-03
 * @version 1.0
 * @desc
 */
func (mapper *SpecialCommentReplyMapper) DelCommentByParentId(parentId uint64, opid uint64, systemId uint64) bool {
	// 获取数据库链接
	mysql := orm.NewOrm()
	// 执行查询
	_, err := mysql.Raw("delete from xk_special_comment_reply where parent_id = ? and opid = ? and system_id = ?", parentId, opid, systemId).Exec()
	if nil != err {
		return false
	}
	return true
}
